package com.transcargo.config;

import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.io.*;

/**
 * Date: 17.02.2011
 * Time: 22:12:32
 */
public class ConfigManager extends HttpServlet implements Serializable
{
	final public static long serialVersionUID = 1;
    private static ConfigManager HINSTANCE = null;
    private Properties properties = null;
    public ConfigManager() throws ServletException {

    	if (HINSTANCE == null) HINSTANCE = this;
    	System.out.println("Initializing ConfigManager...");
        properties = new Properties();
        try {
        	ClassLoader context = Thread.currentThread().getContextClassLoader();
        	InputStream is = context.getResourceAsStream("config.properties");
        	if (is == null) throw new IOException("Unable to find config.properties");
            properties.load(is);
        	System.out.println("Config loaded");
        } catch (IOException e) {
            throw new ServletException("Error reading config.properties: " + e.getMessage());
        }
    }
    public static ConfigManager getInstance() throws ServletException {
        return HINSTANCE != null ? HINSTANCE : new ConfigManager();
    }
    public static String getProperty(String name) throws ServletException {
        return getInstance().properties.getProperty(name);
    }

}
